﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using iDMS.Models;


namespace MvcHtmlHelpers
{
    public enum FileIconSize { px16 = 16, px48 = 48 };

    public static class HtmlHelpers
    {
        //~/Content/Site.css
        public static MvcHtmlString CountryFlag(this HtmlHelper helper, string iso2)
        {
            string path = "/Content/Countries/";
            iso2 = iso2.ToLower();
            TagBuilder img = new TagBuilder("img");
            img.Attributes.Add("src", path + iso2 + ".png");
            img.Attributes.Add("alt", "Country Flag");
            img.Attributes.Add("height", "16");
            img.Attributes.Add("width", "16");
            return new MvcHtmlString(img.ToString());
        }

        private static string fileImagepxTag(string ext, FileIconSize size)
        {
            string path = "/Content/FileIcons/";
            ext = ext.ToLower();
            int px = (int)size;
            TagBuilder img = new TagBuilder("img");
            img.Attributes.Add("src", path + ext + px.ToString() + ".png");
            img.Attributes.Add("alt", "Download File");
            img.Attributes.Add("height", px.ToString());
            img.Attributes.Add("width", px.ToString());
            img.Attributes.Add("border", "0");
            return img.ToString();
        }



        public static MvcHtmlString FileIcon(this HtmlHelper helper, string ext, FileIconSize size)
        {
            return new MvcHtmlString(fileImagepxTag(ext, size));
        }

        //@Html.ActionLinkButton("sss", "SheetDownload", "Drawing", new RouteValueDictionary(new { id = 45 }));

        private static TagBuilder aLink(string innerHtml, string actionName, string controllerName, RequestContext rq, RouteValueDictionary routeValues, string title)
        {
            string href = UrlHelper.GenerateUrl("default", actionName, controllerName, routeValues, RouteTable.Routes, rq, false);
            TagBuilder a = new TagBuilder("a");
            a.Attributes.Add("href", href);
            a.Attributes.Add("title", title);
            a.InnerHtml = innerHtml;
            return a;
        }

        public static MvcHtmlString CustomActionLink(this HtmlHelper htmlHelper, string innerHtml, string actionName, string controllerName, RouteValueDictionary routeValues, string title)
        {

            return new MvcHtmlString(aLink(innerHtml, actionName, controllerName, htmlHelper.ViewContext.RequestContext, routeValues, title).ToString());
        }

        public static MvcHtmlString IconActionLink(this HtmlHelper htmlHelper, string actionName, string controllerName, object routeValues, string title, string iconUI)
        {
            TagBuilder a = aLink("a", actionName, controllerName, htmlHelper.ViewContext.RequestContext, new RouteValueDictionary(routeValues), title);
            a.AddCssClass("ui-icon");
            a.AddCssClass(iconUI);
            TagBuilder div = new TagBuilder("div");
            div.Attributes.Add("style", "display: inline-block");
            div.AddCssClass("ui-state-default ui-corner-all");
            div.AddCssClass("hooverable");
            div.InnerHtml = a.ToString();
            return new MvcHtmlString(div.ToString());
        }

        public static MvcHtmlString ButtonIconActionLink(this HtmlHelper htmlHelper, string actionName, string controllerName, object routeValues, string title, string iconUI)
        {
            TagBuilder span = new TagBuilder("span");
            span.AddCssClass("ui-icon");
            span.AddCssClass(iconUI);
            TagBuilder div = new TagBuilder("div");
            div.Attributes.Add("style", "display: inline-block");           
            div.InnerHtml = span.ToString();

            TagBuilder a = aLink(div.ToString()+title, actionName, controllerName, htmlHelper.ViewContext.RequestContext, new RouteValueDictionary(routeValues), title);
            a.AddCssClass("action");
            return new MvcHtmlString(a.ToString());
        }

        public static MvcHtmlString SheetDownloadActionLink(this HtmlHelper htmlHelper, Sheet sheet)
        {
            return SheetDownloadActionLink(htmlHelper, sheet, FileIconSize.px16);
        }

        public static MvcHtmlString SheetDownloadActionLink(this HtmlHelper htmlHelper, Sheet sheet, FileIconSize size)
        {
            if (sheet.ResourceID != null)
            {
                string shTitle = "Download Sheet: " + sheet.Resource.FileExtension.ToUpper() + " file (" + sheet.FileSize + ")";
                TagBuilder a = aLink(fileImagepxTag(sheet.Resource.FileExtension, size), "SheetDownload", "Drawing", htmlHelper.ViewContext.RequestContext, new RouteValueDictionary(new { id = sheet.ScheetID }), shTitle);
                return new MvcHtmlString(a.ToString());
            }
            return new MvcHtmlString("<span class=\"ui-icon ui-icon-alert\" title=\"Sheet is not yet uploaded!\"></span>");
        }

        public static MvcHtmlString AttachmentDownloadActionLink(this HtmlHelper htmlHelper, SheetAttachment attachment, FileIconSize size)
        {
            //if (attachment.ResourceID != null)
            //{
            string shTitle = "Download Attachment: " + attachment.Resource.FileExtension.ToUpper() + " file (" + attachment.FileSize + ")";
            TagBuilder a = aLink(fileImagepxTag(attachment.Resource.FileExtension, size), "AttachmentDownload", "Drawing", htmlHelper.ViewContext.RequestContext, new RouteValueDictionary(new { id = attachment.AttachmentID }), shTitle);
            return new MvcHtmlString(a.ToString());
            //}
            //return new MvcHtmlString("<span class=\"ui-icon ui-icon-alert\"></span>");
        }

        public static MvcHtmlString AttachmentListActionLink(this HtmlHelper htmlHelper, Sheet sheet)
        {
            if (sheet.SheetAttachments.Any())
            {
                string path = "/Content/Images/";
                TagBuilder img = new TagBuilder("img");
                img.Attributes.Add("src", path + "attachment.png");
                img.Attributes.Add("alt", "Show Attachments");
                img.Attributes.Add("height", "16");
                img.Attributes.Add("width", "16");
                img.Attributes.Add("border", "0");

                string href = "javascript:openDialog('/Drawing/AttachmentsWindow/?id=" + sheet.ScheetID + "',400);";
                TagBuilder a = new TagBuilder("a");
                a.Attributes.Add("href", href);
                a.Attributes.Add("title", "Download Attachments (" + sheet.SheetAttachments.Count + ")");
                a.InnerHtml = img.ToString();
                return new MvcHtmlString(a.ToString());
            }
            else
            {
                if (sheet.InformationCarrier.isAttachmentRequired)
                {
                    return new MvcHtmlString("<span class=\"ui-icon ui-icon-alert\" title=\"Sheet has no attachments, although they are required!\"></span>");
                }
                else
                {
                    return new MvcHtmlString("");
                }
            }
        }

        public static MvcHtmlString DesignChangeDetailsActionLink(this HtmlHelper htmlHelper, DesignChange designChange)
        {
            if (designChange != null)
            {
                string href = "javascript:openDialog('/DesignChange/DetailsWindow/?id=" + designChange.DesignChangeID + "',500);";
                TagBuilder a = new TagBuilder("a");
                a.Attributes.Add("href", href);
                a.Attributes.Add("title", "Design Change Details");
                a.AddCssClass("ui-icon");
                a.AddCssClass("ui-icon-newwin");
                a.InnerHtml = "a";
                TagBuilder div = new TagBuilder("div");
                div.Attributes.Add("style", "display: inline-block");
                div.AddCssClass("ui-state-default ui-corner-all");
                div.AddCssClass("hooverable");
                div.InnerHtml = a.ToString();
                return new MvcHtmlString(div.ToString());
            }
            else
            {
                return new MvcHtmlString("");
            }

        }

        public static MvcHtmlString ExternalActionLink(this HtmlHelper htmlHelper, string url)
        {
            string href = url;
            TagBuilder a = new TagBuilder("a");
            a.Attributes.Add("href", href);
            a.Attributes.Add("title", "Go to: "+url);
            a.AddCssClass("ui-icon");
            a.AddCssClass("ui-icon-extlink");
            a.InnerHtml = "a";
            TagBuilder div = new TagBuilder("div");
            div.Attributes.Add("style", "display: inline-block");
            div.AddCssClass("ui-state-default ui-corner-all");
            div.AddCssClass("hooverable");
            div.InnerHtml = a.ToString();
            return new MvcHtmlString(div.ToString());
        }

        public static MvcHtmlString EmployeeDetailsActionLink(this HtmlHelper htmlHelper, Employee model)
        {
            if (model != null)
            {
                string href = "javascript:openDialog('/Employee/DetailsWindow/?id=" + model.EmployeeID + "',500);";
                TagBuilder a = new TagBuilder("a");
                a.Attributes.Add("href", href);
                a.Attributes.Add("title", "Employee Details");
                a.AddCssClass("ui-icon");
                a.AddCssClass("ui-icon-person");
                a.InnerHtml = "a";
                TagBuilder div = new TagBuilder("div");
                div.Attributes.Add("style", "display: inline-block");
                div.AddCssClass("ui-state-default ui-corner-all");
                div.AddCssClass("hooverable");
                div.InnerHtml = a.ToString();
                return new MvcHtmlString(div.ToString());
            }
            else
            {
                return new MvcHtmlString("");
            }

        }

        public static MvcHtmlString ManufacturerDetailsActionLink(this HtmlHelper htmlHelper, Manufacturer model)
        {
            if (model != null)
            {
                string href = "javascript:openDialog('/Manufacturer/DetailsWindow/?id=" + model.ManufacturerID + "',500);";
                TagBuilder a = new TagBuilder("a");
                a.Attributes.Add("href", href);
                a.Attributes.Add("title", "Manufacturer Details");
                a.AddCssClass("ui-icon");
                a.AddCssClass("ui-icon-newwin");
                a.InnerHtml = "a";
                TagBuilder div = new TagBuilder("div");
                div.Attributes.Add("style", "display: inline-block");
                div.AddCssClass("ui-state-default ui-corner-all");
                div.AddCssClass("hooverable");
                div.InnerHtml = a.ToString();
                return new MvcHtmlString(div.ToString());
            }
            else
            {
                return new MvcHtmlString("");
            }

        }

        public static MvcHtmlString TimePeriod(this HtmlHelper helper, DateTime eventTime)
        {
            string result = "";
            var offlineSince = (DateTime.Now - eventTime);
            if (offlineSince.TotalSeconds <= 60)
            {
                result = "1 minute.";
            }
            else if (offlineSince.TotalMinutes < 60)
            {
                result = Math.Floor(offlineSince.TotalMinutes).ToString() + " minutes";
            }
            else if (offlineSince.TotalMinutes < 120)
            {
                result = "1 hour";
            }
            else if (offlineSince.TotalHours < 24)
            {
                result = Math.Floor(offlineSince.TotalHours).ToString() + " hours.";
            }
            else if (offlineSince.TotalHours < 48)
            {
                result = "1 day";
            }
            else
            {
                result = Math.Floor(offlineSince.TotalDays).ToString() + " days.";
            }
            return new MvcHtmlString(result);
        }

        public static MvcHtmlString UserImage(this HtmlHelper helper, string gender)
        {
            string path = "/Content/Images/";
            string gen = "";
            if (gender == "F")
            {
                gen = "user-female";
            }
            else if (gender == "M")
            {
                gen = "user-male";
            }
            TagBuilder img = new TagBuilder("img");

            img.Attributes.Add("src", path + gen + ".png");
            img.Attributes.Add("alt", "User Image");
            img.Attributes.Add("height", "128");
            img.Attributes.Add("width", "128");
            return new MvcHtmlString(img.ToString());
        }


    }
}

